﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataManager;
using StoreClientModule;
namespace MBB_Store_Sales_Report
{
    public partial class frmCashDenomination : Form
    {
        double amount=0.0f, amt = 0.0f;
        CashRegistered cash = new CashRegistered();
        DataTable RecordTable = new DataTable();
        
        public frmCashDenomination()
        {
            InitializeComponent();

            btnCommit.Click += new EventHandler(btnCommit_Click);
            denoGrid.CellValidated += new DataGridViewCellEventHandler(denoGrid_CellValidated);
            txtOver.KeyPress += new KeyPressEventHandler(txtOver_KeyPress);
            
            btnClose.Click += new EventHandler(btnClose_Click);
           
            panel2.EnabledChanged += new EventHandler(panel2_EnabledChanged);       

            cash.ProcessCompleted += new CashRegistered.EventHandler(cash_ProcessCompleted);
            cash.ProcessError += new CashRegistered.EventHandler(cash_ProcessError);
        }

        void btnCommit_Click(object sender, EventArgs e)
        {

            if (denoGrid.Rows.Count > 0)
            {
                CashRegistryCommit[] crm = new CashRegistryCommit[denoGrid.Rows.Count];

                for (int i = 0; i < denoGrid.Rows.Count; i++)
                {
                    string pcs = denoGrid.Rows[i].Cells[1].Value.ToString();
                    if (pcs.Equals(""))
                        pcs = "0";
                    if (txtOver.Text == "")
                        txtOver.Text = "0";

                    crm[i].fDate = date.Text;
                    crm[i].fDenomination = denoGrid.Rows[i].Cells[0].Value.ToString();
                    crm[i].fPcs = pcs;
                    crm[i].fOver = txtOver.Text;             
                }
                cash.Commit(crm);
                
            }
        }

        void txtOver_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (e.KeyChar == 13)
                {
                    double tOver = double.Parse(txtOver.Text);
                    txtOver.Text = Common.FormatCurrency(tOver);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void cash_ProcessError(string msg)
        {
            uCoreClasses.uThreader t = new uCoreClasses.uThreader();
            t.SafeCall(this, delegate
            {
                MessageBox.Show(msg);
            });

        }

        void cash_ProcessCompleted(string msg)
        {
            uCoreClasses.uThreader t = new uCoreClasses.uThreader();
            t.SafeCall(this, delegate
            {
                if (cash.ProcessCode == 0)
                {
                    RecordTable = cash.RecordTable;
                    denoGrid.DataSource = RecordTable;
                    denoGrid.Columns[0].ReadOnly = true;
                    denoGrid.Columns[2].ReadOnly = true;

                    denoGrid.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

                    txtCheck.Text = Common.FormatCurrency(cash.totalCheck);
                    txtExpensesA.Text = Common.FormatCurrency(cash.totalExA);
                    txtExpensesB.Text = Common.FormatCurrency(cash.totalExB);

                    double.TryParse(txtTotal.Text, out amt);
                    computeTotalCashRegistered(txtOver.Text, amt);
                }
                if (cash.ProcessCode == 1)
                {
                    if (cash.Saved == true)
                        MessageBox.Show("Record Saved");
                    else
                        MessageBox.Show("Record Existing");
                }
            });
        }

        void computeTotalCashRegistered(string over, double amt)
        {
            double totalCashRegistered = 0.0f;
            double tExA = 0.0f;
            double tExB = 0.0f;
            double tCheck = 0.0f;
            double tOver = 0.0f;

            double.TryParse(txtExpensesA.Text, out tExA);
            double.TryParse(txtExpensesB.Text, out tExB);
            double.TryParse(txtCheck.Text, out tCheck);
            double.TryParse(over, out tOver);

            totalCashRegistered = tExA + tExB + tCheck + amt + tOver;
            txtTotalCashRegistered.Text = Common.FormatCurrency(totalCashRegistered);
        }

        void denoGrid_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
           
            var row = denoGrid.Rows[e.RowIndex];
            object denomination = row.Cells[0].Value;
            object pieces = row.Cells[1].Value;
                        
            if (denomination != null && pieces != null )
            {
                double dtype = 0.0;
                if (denomination.ToString().Equals("Coins"))
                    dtype = 1;
                else
                    double.TryParse(denomination.ToString(), out dtype);
                
                    amount = dtype * 1;
                    double pcs = 0.0; 
                    double.TryParse(pieces.ToString(), out pcs);
                    amount = dtype * pcs;
                
                row.Cells[2].Value = Common.FormatCurrency(amount);
            }
            //compute total cash / denomination
            amt = 0.0f;
            for (int i = 0; i < denoGrid.Rows.Count; i++)
            {                
                    double cAmount = 0.0f;
                    object cAmt = denoGrid.Rows[i].Cells[2].Value;
                    if (cAmt != null)
                    {
                        double.TryParse(cAmt.ToString(), out cAmount);
                        amt = amt + cAmount;
                    }                
            }
            txtTotal.Text = Common.FormatCurrency(amt);
            
            //compute for total cash registered
            computeTotalCashRegistered(txtOver.Text, amt);

        }

        void panel2_EnabledChanged(object sender, EventArgs e)
        {
            if (!panel2.Enabled)
            {
                panel2.BackColor = Color.MidnightBlue;
            }
            else
            {
                panel2.BackColor = Color.RoyalBlue;
            }
        }
        void btnClose_Click(object sender, EventArgs e)
        {
            pnlCashDenomination.Dispose();
            this.Dispose();
        }
        public Panel PanelForm
        {
            get { return pnlCashDenomination; }
        }
        public void Initialize(string Title)
        {
            this.Select();
            lblText.Text = Title;
            cash.GetRecords();
        }
    }
}
